import 'package:flutter/material.dart';

class RatingBar extends StatefulWidget {
  final int count;
  final double maxRating;
  final double value;
  final double size;
  final double padding;
  final String normalImage;
  final String selectImage;
  final bool selectAble;
  final ValueChanged<int> onRatingUpdate;

  const RatingBar(
      {super.key,
      this.maxRating = 5.0,
      this.count = 5,
      this.value = 0.0,
      this.size = 20,
      required this.normalImage,
      required this.selectImage,
      required this.padding,
      this.selectAble = false,
      required this.onRatingUpdate});

  @override
  State<RatingBar> createState() => _RatingBarState();
}

class _RatingBarState extends State<RatingBar> {
  double value = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.center,
        child: SizedBox(
          width:
              widget.size * widget.count + widget.padding * (widget.count - 1),
          child: Listener(
            onPointerDown: (PointerDownEvent event) {
              double x = event.localPosition.dx;
              if (x < 0) x = 0;
              pointValue(x);
            },
            onPointerMove: (PointerMoveEvent event) {
              double x = event.localPosition.dx;
              if (x < 0) x = 0;
              pointValue(x);
            },
            behavior: HitTestBehavior.deferToChild,
            child: buildRowRating(),
          ),
        ));
  }

  Widget buildRowRating() {
    return Stack(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: buildNormalRow(),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: buildSelectedRow(),
        )
      ],
    );
  }

  pointValue(double dx) {
    if (!widget.selectAble) {
      return;
    }
    if (dx >=
        widget.size * widget.count + widget.padding * (widget.count - 1)) {
      value = widget.maxRating;
    } else {
      for (double i = 1; i < widget.count + 1; i++) {
        if (dx > widget.size * i + widget.padding * (i - 1) &&
            dx < widget.size * i + widget.padding * i) {
          value = i * ((widget.maxRating / widget.count).ceil());
          break;
        } else if (dx > widget.size * (i - 1) + widget.padding * (i - 1) &&
            dx < widget.size * i + widget.padding * i) {
          value = ((dx - widget.padding * (i - 1)) /
              (widget.size * widget.count) *
              widget.maxRating);
          break;
        }
      }
    }
    setState(() {
      print("结果lvaue----> ${value.ceil()}");
      widget.onRatingUpdate(value.ceil());
    });
  }

  int fullStars() {
    print("fullStars----> $value");
    return (value / (widget.maxRating / widget.count)).ceil();
  }

  List<Widget> buildSelectedRow() {
    int full = fullStars();
    print("full----> $full");
    List<Widget> children = [];
    for (int i = 0; i < full; i++) {
      children.add(Image.asset(
        widget.selectImage,
        height: widget.size,
        width: widget.size,
      ));
      if (i < widget.count - 1) {
        children.add(
          SizedBox(
            width: widget.padding,
          ),
        );
      }
    }
    return children;
  }

  List<Widget> buildNormalRow() {
    List<Widget> children = [];
    for (int i = 0; i < widget.count; i++) {
      children.add(Image.asset(
        widget.normalImage,
        height: widget.size,
        width: widget.size,
      ));
      if (i < widget.count - 1) {
        children.add(SizedBox(
          width: widget.padding,
        ));
      }
    }
    return children;
  }
}
